package a01_数组;

/**
 * <p>
 * a04_有序数组的平方复习7
 * </p>
 *
 * @author flyduck
 * @since 2025/5/26
 */
public class a04_有序数组的平方复习7 {
    //输入：nums = [-4,-1,0,3,10]
    //输出：[0,1,9,16,100]
    //解释：平方后，数组变为 [16,1,0,9,100]
    //排序后，数组变为 [0,1,9,16,100]
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int resultIdx = result.length - 1;

        int leftIdx = 0;
        int rightIdx = nums.length - 1;
        while (leftIdx <= rightIdx){
            if(nums[leftIdx] * nums[leftIdx] >= nums[rightIdx] * nums[rightIdx]){
                result[resultIdx] = nums[leftIdx] * nums[leftIdx];
                leftIdx++;
            }else {
                result[resultIdx] = nums[rightIdx] * nums[rightIdx];
                rightIdx--;
            }
            resultIdx--;
        }
        return result;
    }
}
